// 检测用户手机可用地图
function checkCanUseMap() {
	let mapArray = []


	//#ifndef MP-WEIXIN
	if (plus.os.name == "Android") {
		if (plus.runtime.isApplicationExist({
				pname: 'com.baidu.BaiduMap'
			})) {
			mapArray.push({
				name: '百度地图',
				type: 1,
				phone: 'Android'
			})
		}
		if (plus.runtime.isApplicationExist({
				pname: 'com.autonavi.minimap'
			})) {
			mapArray.push({
				name: '高德地图',
				type: 2,
				phone: 'Android'
			})
		}
		if (plus.runtime.isApplicationExist({
				pname: 'com.tencent.map'
			})) {
			mapArray.push({
				name: '腾讯地图',
				type: 3,
				phone: 'Android'
			})
		}
		// 增加默认地图
		mapArray.push({
			name: '查看位置',
			type: 4,
			phone: 'Android'
		})
	} else {
		if (plus.runtime.isApplicationExist({
				action: 'baidumap://'
			})) {
			mapArray.push({
				name: '百度地图',
				type: 1,
				phone: 'ios'
			})
		}
		if (plus.runtime.isApplicationExist({
				action: 'iosamap://'
			})) {
			mapArray.push({
				name: '高德地图',
				type: 2,
				phone: 'ios'
			})
		}
		if (plus.runtime.isApplicationExist({
				action: 'qqmap://'
			})) {
			mapArray.push({
				name: '腾讯地图',
				type: 3,
				phone: 'ios'
			})
		}
		// 增加默认地图
		mapArray.push({
			name: '查看位置',
			type: 4,
			phone: 'ios'
		})
	}
	//#endif
	//#ifdef MP-WEIXIN
	mapArray.push({
		name: '查看位置',
		type: 4,
		phone: '小程序'
	})
	//#endif


	return mapArray
}

function openMapByAndroid(latitude, longitude, name, type) {
	let url = ''; // 回调地址
	let identity = ''; // 程序名称
	switch (type) {
		case 1:
			url =
				`baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`
			identity = 'com.baidu.BaiduMap'
			this.openURL(url, identity)
			break;
		case 2:
			url =
				`androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`
			identity = 'com.autonavi.minimap'
			this.openURL(url, identity)
			break;
		case 3:
			url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=SKABZ-LZZW5-VL7IR-IUYJA-L73D6-KMBO6`;
			// com.tencent.map
			identity = 'com.tencent.map'
			this.openURL(url, identity)
			break;
		default:
			this.openMapByDefault(latitude, longitude, name)
			break;
	}
}

function openMapByIos(latitude, longitude, name, type) {
	let url = ''; // 回调地址
	let errorCB = ''; // url失败的回调地址
	let identity = ''; // 程序名称
	switch (type) {
		case 1:
			//注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
			url =
				`baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
			this.openURL(url, identity)
			break;
		case 2:
			url =
				`iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`
			this.openURL(url, identity)
			break;
		case 3:
			url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=SKABZ-LZZW5-VL7IR-IUYJA-L73D6-KMBO6`;
			this.openURL(url, identity)
			break;
		default:
			this.openMapByDefault(latitude, longitude, name)
			break;
	}
}

// 打开默认地图
function openMapByDefault(latitude, longitude, name) {
	uni.openLocation({
		latitude: latitude,
		longitude: longitude,
		name: name,
		fail: () => {
			uni.showModal({
				content: '打开地图失败,请重试！'
			})
		},
	})
}

function openURL(url, identity) {
	let newurl = encodeURI(url);
	plus.runtime.openURL(newurl, function(res) {
		uni.showModal({
			content: res.message
		})
	}, identity);
}


module.exports = {
	checkCanUseMap,
	openMapByAndroid,
	openMapByIos,
	openMapByDefault,
	openURL
}
